using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;

namespace Exem
{
	public static class AccessHelper
	{
		public static readonly string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + "\\DB\\Jsy_Data.mdb";

		private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

		public static int ExecuteNonQuery(string cmdText, params OleDbParameter[] commandParameters)
		{
			OleDbCommand cmd = new OleDbCommand();
			using (OleDbConnection conn = new OleDbConnection(connStr))
			{
				PrepareCommand(cmd, conn, null, cmdText, commandParameters);
				int val = cmd.ExecuteNonQuery();
				cmd.Parameters.Clear();
				return val;
			}
		}

		public static int ExecuteNonQuery(OleDbConnection connection, string cmdText, params OleDbParameter[] commandParameters)
		{
			OleDbCommand cmd = new OleDbCommand();
			PrepareCommand(cmd, connection, null, cmdText, commandParameters);
			int val = cmd.ExecuteNonQuery();
			cmd.Parameters.Clear();
			return val;
		}

		public static int ExecuteNonQuery(OleDbTransaction trans, string cmdText, params OleDbParameter[] commandParameters)
		{
			OleDbCommand cmd = new OleDbCommand();
			PrepareCommand(cmd, trans.Connection, trans, cmdText, commandParameters);
			int val = cmd.ExecuteNonQuery();
			cmd.Parameters.Clear();
			return val;
		}

		public static OleDbDataReader ExecuteReader(string cmdText, params OleDbParameter[] commandParameters)
		{
			OleDbCommand cmd = new OleDbCommand();
			OleDbConnection conn = new OleDbConnection(connStr);
			try
			{
				PrepareCommand(cmd, conn, null, cmdText, commandParameters);
				OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
				cmd.Parameters.Clear();
				return reader;
			}
			catch
			{
				conn.Close();
				throw;
			}
		}

		public static DataTable ExecuteDataSet(string cmdText, params OleDbParameter[] commandParameters)
		{
			OleDbCommand cmd = new OleDbCommand();
			using (OleDbConnection conn = new OleDbConnection(connStr))
			{
				PrepareCommand(cmd, conn, null, cmdText, commandParameters);
				OleDbDataAdapter da = new OleDbDataAdapter(cmd);
				DataTable dt = new DataTable();
				try
				{
					da.Fill(dt);
					cmd.Parameters.Clear();
					return dt;
				}
				catch
				{
					conn.Close();
					throw;
				}
			}
		}

		public static object ExecuteScalar(string cmdText, params OleDbParameter[] commandParameters)
		{
			OleDbCommand cmd = new OleDbCommand();
			using (OleDbConnection connection = new OleDbConnection(connStr))
			{
				PrepareCommand(cmd, connection, null, cmdText, commandParameters);
				object val = cmd.ExecuteScalar();
				cmd.Parameters.Clear();
				return val;
			}
		}

		public static object ExecuteScalar(OleDbConnection connection, string cmdText, params OleDbParameter[] commandParameters)
		{
			OleDbCommand cmd = new OleDbCommand();
			PrepareCommand(cmd, connection, null, cmdText, commandParameters);
			object val = cmd.ExecuteScalar();
			cmd.Parameters.Clear();
			return val;
		}

		public static void CacheParameters(string cacheKey, params OleDbParameter[] commandParameters)
		{
			parmCache[cacheKey] = commandParameters;
		}

		public static OleDbParameter[] GetCachedParameters(string cacheKey)
		{
			OleDbParameter[] cachedParms = (OleDbParameter[])parmCache[cacheKey];
			if (cachedParms == null)
			{
				return null;
			}
			OleDbParameter[] clonedParms = new OleDbParameter[cachedParms.Length];
			int j = 0;
			for (int i = cachedParms.Length; j < i; j++)
			{
				clonedParms = (OleDbParameter[])((ICloneable)cachedParms).Clone();
			}
			return clonedParms;
		}

		private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, string cmdText, OleDbParameter[] cmdParms)
		{
			if (conn.State != ConnectionState.Open)
			{
				conn.Open();
			}
			cmd.Connection = conn;
			cmd.CommandText = cmdText;
			if (trans != null)
			{
				cmd.Transaction = trans;
			}
			cmd.CommandType = CommandType.Text;
			if (cmdParms != null)
			{
				foreach (OleDbParameter parm in cmdParms)
				{
					cmd.Parameters.Add(parm);
				}
			}
		}
	}
}
